package com.kingdee.eas.custom.tool;

import java.util.ArrayList;
import java.util.List;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.eventbus.event.action.IActionEventHandler;
import com.kingdee.bos.eventbus.event.action.KDActionEvent;
import com.kingdee.bos.webframework.dynamic.handler.AbstractWebEventHandler;
import com.kingdee.eas.common.EASBizException;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.webframework.json.JSONUtils;
import com.kingdee.eas.basedata.assistant.ProjectCollection;
import com.kingdee.eas.basedata.assistant.ProjectFactory;
import com.kingdee.eas.basedata.assistant.ProjectInfo;
import com.kingdee.util.StringUtils;

public class GetChildNodesHandler extends AbstractWebEventHandler implements IActionEventHandler {
	@Override
	public void onAction(KDActionEvent event) throws EASBizException,
			BOSException {
		String errorMsg = "true";
		//左树要隐藏的节点
		List<String> hideNode = new ArrayList<String>();
//		hideNode.add("设备购置-预算小计");
//		hideNode.add("设备购置-预算");
		
		HttpServletRequest request = event.getReqeustContext().getHttpServletRequest();
		String treeHideNodeStr = request.getParameter("treeHideNode");
		if (treeHideNodeStr != null) {
			String[] treeHideNode = treeHideNodeStr.split(",");
			for (int i = 0; i < treeHideNode.length; i++) {
				hideNode.add(treeHideNode[i]);
			}
		}

		
		String parentId = request.getParameter("parentId");//关于参数，参看下方说明①
		JSONArray jsonArr = new JSONArray(900);
		if(StringUtils.isEmpty(parentId)){
			//如果parentId为空，则生成根节点，以及根节点下的第一级节点，并且初始化展开状态
			addNode(jsonArr, "root", "", "预算项目", "", false);
			//查询根节点下的第一级节点，即level=1
			EntityViewInfo entityViewInfo = new EntityViewInfo();
			FilterInfo filter=new FilterInfo(); 

			filter.getFilterItems().add(new FilterItemInfo("level","1"));			
			entityViewInfo.setFilter(filter);
			ProjectInfo projectInfo =null;
			ProjectCollection projectCollection = null;
			try {
				projectCollection = ProjectFactory.getRemoteInstance().getProjectCollection(entityViewInfo);
			} catch (Exception e) {
				JSONUtils.ERROR(e); 
			}
			if (projectCollection != null) {
				for(int i = projectCollection.size() - 1; i >= 0; i--){
					 projectInfo = projectCollection.get(i);
					 //新增节点
					 addNode(jsonArr,projectInfo.getId().toString(),projectInfo.getLongNumber(), projectInfo.getName(), parentId, projectInfo.isIsLeaf());
				}
			}
		}
		else{
			/*
			 * 如果parentId不为空，则根据此ip，查询所有记录中parent.id = parentId的记录
			 */
			EntityViewInfo evi = new EntityViewInfo();
			FilterInfo fi = new FilterInfo();
			fi.getFilterItems().add(new FilterItemInfo("parent.id", parentId, CompareType.EQUALS));			
			evi.setFilter(fi );
			ProjectCollection ppColl = ProjectFactory.getRemoteInstance().getProjectCollection(evi);
			if(ppColl != null){
				for(int i = 0; i < ppColl.size(); i++){
					ProjectInfo project = ppColl.get(i);
					//隐藏指定的节点，不必生成。此处是HIDDEN_COL
					if(hideNode.contains(project.getName())){
						continue;
					}
					addNode(jsonArr, project.getId().toString(), project.getLongNumber(), project.getName(), parentId, Boolean.valueOf(project.get("isLeaf").toString()));
				}
			}
		}
		ProjectFactory.getRemoteInstance().getCollection();
		JSONUtils.SUCCESS(jsonArr);

	}
	/**
	 * 描述：添加节点
	 * @param jsonArr 用于村粗节点的数组
	 * @param id 节点id
	 * @param name  节点名称，即显示文字
	 * @param parentId  父节点id
	 * @param isLeaf 是否为叶子节点，true则指当前节点不再有子节点，否则应为false
	 */
	private void addNode(JSONArray jsonArr, String id, String number, String name,
			String parentId, Boolean isLeaf) {
		JSONObject node = new JSONObject();
		node.put("id", id);
		node.put("longnumber", number);
		node.put("name", name);
		node.put("pid", parentId);
		node.put("isLeaf", Boolean.valueOf(isLeaf));
		/* 说明②
		 * 如需要其他属性，则需要自己在子模块的js中重写_addTreeNodes方法，否则在这个地方加进去了，前端也取不到
		 */
		jsonArr.add(node);
	}


}